package com.cskaoyan.market.service.admin;

import com.cskaoyan.market.db.domain.MarketBrand;
import com.cskaoyan.market.db.domain.MarketBrandExample;
import com.cskaoyan.market.db.mapper.MarketBrandMapper;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MarketBrandServiceImpl implements MarketBrandService {

    @Autowired
    MarketBrandMapper marketBrandMapper;

    @Override
    public List<MarketBrand> list(Integer page, Integer limit, Integer id, String name, String sort, String order) {
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        marketBrandExample.setOrderByClause(sort + " " + order);
        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();
        if (id != null) {
            criteria.andIdEqualTo(id);
        }
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andLogicalDeleted(true);

        PageHelper.startPage(page, limit);
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(marketBrandExample);
        return marketBrands;
    }

    @Override
    public void create(MarketBrand marketBrand) {
        marketBrandMapper.insert(marketBrand);
    }

    @Override
    public void update(MarketBrand marketBrand) {
        marketBrandMapper.updateByPrimaryKeySelective(marketBrand);
    }

    @Override
    public void delete(MarketBrand marketBrand) {
        marketBrandMapper.logicalDeleteByPrimaryKey(marketBrand.getId());
    }
}
